#!/usr/bin/perl -w

use Data::Dumper;
use Crypt::RSA; 
use Convert::ASCII::Armour;

	$test_string="This is a test string to key RSA encryption.\n";
	# Load in public/private keys if exist...

	if(! -f "testkey.public") {
		$rsa=new Crypt::RSA;
		($public, $private) = $rsa->keygen( Size => 512, Armour => 1 ); 

		$public->write(Filename => "testkey.public");
		$private->write(Filename => "testkey.private");
#		$message   = $converter->armour(
#			Headers  => {
#					"Version" => '1'
#				},
#			Object   => "LHA Public key",
#			Content  => $public,
#			Compress => 0,
#		);
#		open(FD,">key.public");
#		print FD $message;
#		close(FD);
#		$message   = $converter->armour(
#			Headers  => {
#					"Version" => '1'
#				},
#			Object   => "LHA Private key",
#			Content  => $private,
#			Compress => 0,
#		);
#		open(FD,">key.private");
#		print FD $message;
#		close(FD);

		$rsa=new Crypt::RSA;
		$msg=$rsa->encrypt(
			Message	=> $test_string,
			Key	=> $public,
			Armour  => 1);

		if(!defined($msg)) {
			print "Error: ",$rsa->errstr,"\n";
			exit 1;
		}
		print $msg; 
	}

	$public  = new Crypt::RSA::Key::Public (Filename => 'testkey.public');
	$private = new Crypt::RSA::Key::Private (Filename => 'testkey.private');
	# print Dumper($public);
	# print Dumper($private);

	die("Unable to load public key.") if !defined($public);
	die("Unable to load private key.") if !defined($private);
	$rsa=new Crypt::RSA;
	$msg=$rsa->encrypt(
		Message	=> $test_string,
		Key	=> $public,
		Armour  => 0);

	if(!defined($msg)) {
		print "Error: ",$rsa->errstr,"\n";
		exit 1;
	}
	# print $msg; 

	# print "\n\n==============\n\n";

	$msg2=$rsa->decrypt(
		Cyphertext	=> $msg,
		Key	=> $private,
		Armour  => 0);

	if(!defined($msg)) {
		print "Error: ",$rsa->errstr,"\n";
		exit 1;
	}
	if($msg2 eq $test_string) {
		print "RSA Encryption/Decryption test worked.\n";
	} else {
		print "RSA Encryption/Decryption test failed!\n";
	}

